/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.common.utils.BeanUtils;
import com.tbit.main.constant.enums.MachineAlarmEnum;
import com.tbit.main.pojo.GeoreMachine;
import com.tbit.main.pojo.Machine;
import com.tbit.main.pojo.MachineAlarmLog;
import com.tbit.main.pojo.SmsSendMessage;
import com.tbit.main.pojo.VO.AccountUserAlarmRespVO;
import com.tbit.main.pojo.bo.MachineAlarmBO;
import com.tbit.main.service.*;
import com.tbit.main.sms.enums.template.SmsCodeTemplateEnum;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author <a href="mailto:li.yao@tbit.com.cn">yao</a>
 * @version 4.1.0
 * @since 2024/7/16 9:01
 */
@Service
public class SmsSendAlarmServiceImpl implements SmsSendAlarmService {

    @Resource
    private AccountUserAlarmService accountUserAlarmService;
    @Resource
    private SmsSendService smsSendService;
    @Resource
    private GeoreMachineService georeMachineService;
    @Resource
    private MachineAlarmLogService machineAlarmLogService;
    @Resource
    private DataService dataService;

    @Override
    public void sendSmsAlarm(String accountName, Machine machine, MachineAlarmEnum machineAlarmEnum) {
        Integer accountId = machine.getAccountId();
        List<AccountUserAlarmRespVO> accountUserAlarms = accountUserAlarmService.getByAccountId(accountId);

        /* 发送短信*/
        for (AccountUserAlarmRespVO accountUserAlarm : accountUserAlarms) {
            if (CollUtil.isNotEmpty(accountUserAlarm.getAlarmItem())
                    && accountUserAlarm.getAlarmItem().contains(machineAlarmEnum.getAlarmType())) {
                MachineAlarmBO machineAlarm = new MachineAlarmBO()
                        .setAccountName(accountName)
                        .setUserCode(machine.getUserCode())
                        .setAlarmDesc(machineAlarmEnum.getAlarmDesc());

                Map<String, Object> beanMap = BeanUtils.beanToMap(machineAlarm);

                smsSendService.sendSms(new SmsSendMessage()
                        .setTemplateCode(SmsCodeTemplateEnum.AlARM.getCode())
                        .setMobile(accountUserAlarm.getPhone())
                        .setTemplateParams(beanMap)
                        .setBrandId(dataService.getParentId(accountId)));
            }
        }

        /* 添加预警日志*/
        MachineAlarmLog machineAlarmLog = new MachineAlarmLog();
        machineAlarmLog.setAccountId(accountId);
        machineAlarmLog.setAlarmType(machineAlarmEnum.getAlarmType());
        machineAlarmLog.setUserCode(machine.getUserCode());
        machineAlarmLog.setAlarmTime(DateTimeUtil.getNowTime());
        machineAlarmLog.setRemark("");

        /*
         * 判断分区
         */
        GeoreMachine georeMachine = georeMachineService.getByMachineId(machine.getMachineId());
        if (georeMachine != null) {
            machineAlarmLog.setRegionIds(georeMachine.getRegionId());
        }
        machineAlarmLogService.insert(machineAlarmLog);
    }
}
